</html>
<SCRIPT LANGUAGE="JavaScript">

function OnDivScroll()
{
	var lstCollegeNames = document.getElementById("G");
	
	if (lstCollegeNames.options.length > 63)
	{
		lstCollegeNames.size=lstCollegeNames.options.length;
	}
	else
	{
		lstCollegeNames.size=63;	
	}	
}

	//On focus of Selectbox
	function OnSelectFocus()
	{
		
		
		if (document.getElementById("divCollegeNames").scrollLeft != 0)
		{
			document.getElementById("divCollegeNames").scrollLeft = 0;
		}


		var lstCollegeNames = document.getElementById('G');
		
		if( lstCollegeNames.options.length > 63)
		{
			lstCollegeNames.focus();
			lstCollegeNames.size=63;
		}
}

</script>
<?php



$debug=0;
$dbhost = "localhost";
$dbuser = "fossilplot";
$dbpass = "fossilplot";
$dbname = "fossilplot";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());

$arrGen = $_POST['arrGen'];
$arrCart = $_POST['arrCart'];
//$Make = $_POST['Make'];


if($debug){
echo "hello selected Gen ".$arrGen."<br>";
//echo "hello Cart ".str_replace('*','+',$arrCart)."<br>";
}


$exploded_cart = explode(',', $arrCart);

$exploded_gen = explode(',', $arrGen);

if($exploded_gen[0]){
	$tmp="; ".$exploded_gen[0].","; //in arrCart each id is between "; " and "," except for the last one
	$pos=strpos($arrCart,$tmp);
	$last=substr($arrCart, strrpos($arrCart,";")+2); //this is for the case that the cart's last genus's id is equal the newly added one, +2 because there is a blank after ; character.
	
	if(($pos !== false) ||($last==$exploded_gen[0])){
		$search_gen="(";
		$j=0;
	
	}
	else{
		$search_gen="('".$exploded_gen[0]."'";
		$j=1;
	}
}
for($i=1;$i<sizeof($exploded_gen);$i++){

		$tmp="; ".$exploded_gen[$i].",";
		$pos=strpos($arrCart,$tmp);
		
		$last=substr($arrCart, strrpos($arrCart,";")+2);
		if(($pos !== false) || ($last == $exploded_gen[$i])){next;}
		else{
			if($search_gen=="("){
				$search_gen=$search_gen."'".$exploded_gen[$i]."'";
				$j++;
			}else{
	
				$search_gen=$search_gen.","."'".$exploded_gen[$i]."'";
				$j++;
			}
		}

}
$search_gen=$search_gen.")";

//echo "search_gen is $search_gen";


if($debug){
echo "exploded gen 3 is ".$exploded_gen[0]."<br>";
echo "size of selected gen is ".sizeof($exploded_gen)."<br>";
}
ob_flush(); flush();
if($exploded_cart[0]!="No Items Selected") $len=$j+sizeof($exploded_cart);
else $len=$j;
echo "<div id='countdiv'>
          
         
            <font size=2  color='993333'>$len genera are selected.</font>
 </div>";

//echo "<input type=\"hidden\" id=\"showphy\" name=\"showphy\" value='$Make'/>";
//echo "<input type=\"hidden\" id=\"showord\" name=\"showord\" value='$arrOrder'/>";
if ($arrGen == null && $exploded_cart[0]=="No Items Selected"){
	
		echo "<select name=\"G[]\" multiple size=\"63\" style=\"WIDTH: 290px\">";
		echo "<option>"."No Items Selected"."</option>";
		echo "</select>";
	
	
}
else{ //$arrGen is not null
//echo "cart is $exploded_cart[0]";

//echo "<div id=\"select_container\" style=\"overflow-x:scroll; width:290px; overflow: -moz-scrollbars-horizontal;\" >";
//echo "<select name=\"G[]\" multiple size=\"63\">";

echo "<div id=\"divCollegeNames\" style=\"OVERFLOW: auto;WIDTH: 304px;HEIGHT: 1007px\" onscroll=\"OnDivScroll();\">";
echo "<SELECT id=\"G\" name=\"G[]\" size=\"63\" multiple=\"\" onfocus=\"OnSelectFocus();\">";

foreach($exploded_cart as $tmp_cart){
if($tmp_cart != "No Items Selected"){
echo "<option>".$tmp_cart."</option>";
}

}


$result = mysql_query("select id, genus_name, phylum_name, class_name, order_name,fad_date,lad_date from genus where id in $search_gen");

while ($row = mysql_fetch_array($result)) {
	echo "<option>".$row{'genus_name'}."; ".$row{'phylum_name'}."; ".$row{'class_name'}.";".$row{'order_name'}."; ".$row{'fad_date'}."; ".$row{'lad_date'}."; ".$row{'id'}."</option>";
}




echo "</select>";

echo "</div>";
} //end of else ($arrGen is not null)


?>
</html>